package com.billdowney.service.impl.system;

import java.io.File;
import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import com.billdowney.dao.system.SystemDataDictDao;
import com.billdowney.entity.system.SystemDataDict;
import com.billdowney.exception.BizException;
import com.billdowney.service.impl.base.BaseServiceImpl;
import com.billdowney.service.interfaces.system.SystemDataDictService;
import com.billdowney.service.util.system.SystemDataDictUtil;
import com.billdowney.service.util.system.SystemDataDictConst;
import com.billdowney.util.CommonUtil;
import com.billdowney.util.file.FileUtil;

/**
 * @Description: 系统字典接口实现
 * @author: 唐路钧
 * @date: 2018年9月6日 下午4:46:53
 */
@Service
public class SystemDataDictServiceImpl extends BaseServiceImpl<SystemDataDictDao, SystemDataDict, String>
		implements SystemDataDictService {

	@Autowired
	private SystemDataDictUtil systemParamsUtil;
	
	@Resource(type = SystemDataDictDao.class)
	@Override
	public void setDao(SystemDataDictDao dao) {
		super.dao = dao;
	}

	@Override
	public SystemDataDict saveOrUpdate(SystemDataDict systemDataDict) throws Exception {
		if (CommonUtil.isEmpty(systemDataDict.getKeyName())) {
			throw new BizException(10401);
		} else if (CommonUtil.isEmpty(systemDataDict.getName())) {
			throw new BizException(10403);
		}
		return dao.save(systemDataDict);
	}

	@Override
	public SystemDataDict findById(String id) throws Exception {
		Optional<SystemDataDict> systemDataDict = dao.findById(id);
		if (systemDataDict.isPresent()) {
			throw new BizException(10402);
		}
		return systemDataDict.get();
	}

	@Override
	public boolean validKeyIsUse(String key) throws Exception {
		SystemDataDict dictQuery = new SystemDataDict();
		dictQuery.setKeyName(key);
		return dao.exists(Example.of(dictQuery));
	}

	@Override
	public String getString(String key) throws Exception {
		SystemDataDict dictQuery = new SystemDataDict();
		dictQuery.setKeyName(key);
		Optional<SystemDataDict> systemDataDict = dao.findOne(Example.of(dictQuery));
		SystemDataDict dict = null;
		// 如果为空就去查询默认配置的数据字典文件
		if (systemDataDict.isPresent()) {
			dict = systemDataDict.get();
		} else {
			dict = systemParamsUtil.get(key);
			if (CommonUtil.isEmpty(dict)) {
				dict = new SystemDataDict();
				dict.setKeyName(key);
				dict.setDescription("未在数据字典文件配置默认值");
			}
			if (CommonUtil.isEmpty(dict.getDescription())) {
				dict.setDescription("未在数据字典文件中找到相关描述");
			}
			this.saveOrUpdate(dict);
		}
		return dict.getContent();
	}

	@Override
	public String getDefaultFilePath() {
		String path = "";
		try {
			path = this.getString(SystemDataDictConst.defaultFilePath);
			if (!FileUtil.createDir(path)) {
				throw new Exception();
			}
			if (!path.endsWith(File.separator)) {
				path += "/";
			}
		} catch (Exception e) {
			e.printStackTrace();
			path = System.getProperty("user.home");
			if (path.endsWith(File.separator)) {
				path += "TomcatWebFiles/";
			} else {
				path += "/TomcatWebFiles/";
			}
		}
		return path;
	}

}
